package cn.edu.nju.ws.sview.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.views.Image;
import cn.edu.nju.ws.sview.views.User;

import com.hp.hpl.jena.sparql.lib.org.json.JSONArray;

/**
 * Servlet implementation class ImageServlet
 */
@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String option=null;
		String json=null;
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		if(request.getParameter("getImages")!=null){
			json = request.getParameter("getImages");
			option="getImages";
		}
		else if(request.getParameter("addImage")!=null){
			json = request.getParameter("addImage");
			option="addImage";
		}
		
		if(option.equals("getImages")){
			String str = URLDecoder.decode(json,"utf-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int etype = Integer.parseInt((String)regtJSON.get("etype")) ;
			int eid = Integer.parseInt((String)regtJSON.get("eid")) ;
			String lang = (String)regtJSON.get("lang") ;
			String groupName = (String)regtJSON.get("groupName") ;
			String type = (String)regtJSON.get("type") ;
			int start = ((Long)regtJSON.get("start")).intValue();
			int length = ((Long)regtJSON.get("length")).intValue();
			
			int totalNum = 0 ;
			if(type.equals("all")){
				totalNum = Image.getImagesTotalNumByEntityGroup(etype, eid, null);
			}else if(type.equals("mine")){
				totalNum = Image.getImagesTotalNumByEntityGroup(etype, eid, groupName);
			}
			ArrayList<Image> images = Image.getImagesByEntityGroupOrderByTime(etype, eid, groupName, start, length);
			JSONArray  dataArray = new JSONArray();
			int userID = 0;
			if(groupName!=null)
				userID = User.getUserIdByName(groupName) ;
			for(Image image:images){
				JSONObject obj = new JSONObject() ;
				obj.put("id", image.getID());
				obj.put("user", image.getGroupName());
				long time = image.getTime();
				Date date = new Date(time);
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String timeStr = format.format(date) ;
				obj.put("time", timeStr) ;
				obj.put("imgPath", image.getPath()) ;
				
				dataArray.put(obj) ;
			}
			
			JSONObject result = new JSONObject();
			result.put("totalNum", totalNum) ;
			result.put("data", dataArray);
			response.getWriter().print(result) ;
 		}
		else if(option.equals("addImage")){
			String str = URLDecoder.decode(json,"utf-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int etype = Integer.parseInt((String)regtJSON.get("etype")) ;
			int eid = Integer.parseInt((String)regtJSON.get("eid")) ;
			String groupName = (String)regtJSON.get("groupName") ;
			String path = (String)regtJSON.get("path") ;
			
			Image image = new Image(groupName,System.currentTimeMillis(),path,etype,eid);
			image.serialize();
		}
	}

}
